/*
Copyright (C) 2012 John Tsiombikas <nuclear@member.fsf.org>,
                   Ioannis Makris <imak@imak.gr>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#import "glview.h"
#include "glory.h"
#include "gloin.h"
#include "wsys.h"

static void handle_key(struct window *win, NSEvent *ev, int state);
static void handle_mouse(struct window *win, NSEvent *ev, int state);
static void handle_motion(struct window *win, NSEvent *ev, int drag);

@implementation GloinGLView

-(id) initWithFrame: (NSRect) frame pixelFormat: (NSOpenGLPixelFormat*) pf
{
	self = [super initWithFrame: frame pixelFormat: pf];
	return self;
}

-(void) drawRect: (NSRect) rect
{
	gloin_wsys_redisplay(win);
}

-(void) reshape
{
	int win_xsz, win_ysz;
	NSSize sz;
	sz = [self bounds].size;

	gloin_wsys_get_size(win, &win_xsz, &win_ysz);

	if(sz.width != win_xsz || sz.height != win_ysz) {
		void (*func)();

		gloin_wsys_set_size(win, sz.width, sz.height);

		if((func = gloin_get_callback(win, GLORY_RESHAPE))) {
			gloin_set_active(win);
			func(sz.width, sz.height, gloin_get_callback_data(win, GLORY_RESHAPE));
		}
	}
}

-(void) keyDown: (NSEvent*) ev
{
	handle_key(win, ev, 1);
}

-(void) keyUp: (NSEvent*) ev
{
	handle_key(win, ev, 0);
}

-(void) mouseDown: (NSEvent*) ev
{
	handle_mouse(win, ev, 1);
}

-(void) mouseUp: (NSEvent*) ev
{
	handle_mouse(win, ev, 0);
}

-(void) rightMouseDown: (NSEvent*) ev
{
	handle_mouse(win, ev, 1);
}

-(void) rightMouseUp: (NSEvent*) ev
{
	handle_mouse(win, ev, 0);
}

-(void) otherMouseDown: (NSEvent*) ev
{
	handle_mouse(win, ev, 1);
}

-(void) otherMouseUp: (NSEvent*) ev
{
	handle_mouse(win, ev, 0);
}

-(void) mouseDragged: (NSEvent*) ev
{
	handle_motion(win, ev, 1);
}

-(void) rightMouseDragged: (NSEvent*) ev
{
	handle_motion(win, ev, 1);
}

-(void) otherMouseDragged: (NSEvent*) ev
{
	handle_motion(win, ev, 1);
}


-(BOOL) acceptsFirstResponder
{
	return YES;
}
@end

static void handle_key(struct window *win, NSEvent *ev, int state)
{
	NSString *str;
	void (*func)();

	if((func = gloin_get_callback(win, GLORY_KEYBOARD))) {
		str = [ev characters];
		if([str length]) {
			unichar c = [str characterAtIndex: 0];

			gloin_set_active(win);
			func(c, state, gloin_get_callback_data(win, GLORY_KEYBOARD));
		}
	}
}

static void handle_mouse(struct window *win, NSEvent *ev, int state)
{
	void (*func)();

	if((func = gloin_get_callback(win, GLORY_MOUSE))) {
		NSPoint pt;
		int bn = [ev buttonNumber];
		if(bn == 2) {
			bn = 1;
		} else if(bn == 1) {
			bn = 2;
		}
		pt = [ev locationInWindow];

		win->mouse_index = 0;

		gloin_set_active(win);
		func(bn, state, pt.x, pt.y, gloin_get_callback_data(win, GLORY_MOUSE));
	}
}

static void handle_motion(struct window *win, NSEvent *ev, int drag)
{
	void (*func)();
	int cbidx = drag ? GLORY_MOTION : GLORY_PASSIVE;

	if((func = gloin_get_callback(win, cbidx))) {
		NSPoint pt = [ev locationInWindow];

		win->mouse_index = 0;

		gloin_set_active(win);
		func(pt.x, pt.y, gloin_get_callback_data(win, cbidx));
	}
}
